package cn.com.yx.ms.controller;

import cn.com.yx.ms.application.web.ApiPager;
import cn.com.yx.ms.application.web.ApiQuery;
import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.dbmodel.GgMain;
import cn.com.yx.ms.domain.dbmodel.JmProgram;
import cn.com.yx.ms.domain.dbmodel.ScFile;
import cn.com.yx.ms.domain.gg.GgMainModel;
import cn.com.yx.ms.domain.program.CatalogModel;
import cn.com.yx.ms.domain.program.ProgramModel;
import cn.com.yx.ms.domain.program.UploadFileModel;
import cn.com.yx.ms.service.IProgramService;
import cn.yx.framework.configuration.ApiCode;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.uploader.Plupload;
import cn.yx.framework.uploader.PluploadService;
import cn.yx.framework.util.MsgPropertyUtil;
import cn.yx.framework.view.ApiResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * 应用中心
 */
@RestController
@RequestMapping(value = "/")
@Api(value = "节目管理", description = "获取或操作节目资源")
public class ProgramController {

    private static final Logger logger = LoggerFactory.getLogger(ProgramController.class);

    @Resource
    private MessageSource messageSource;

    @Resource
    private IProgramService iProgramService;
    @Resource
    private RedisTemplate redisTemplate;


    // region << 节目管理 >>
    @PostMapping("program")
    @ApiOperation(value = "创建节目信息", notes = "创建节目信息")
    @ApiImplicitParam(paramType = "body", name = "programModel", dataType = "Map", required = true, value = "节目实体详细信息")
    public ApiResult create(@RequestBody @Validated ProgramModel programModel, BindingResult bindingResult) {
        ApiCode apiCode = iProgramService.saveProgram(programModel);
        return new ApiResult<>(Integer.parseInt(apiCode.msgId),
                apiCode.equals(ApiCode.APP_300500),
                MsgPropertyUtil.getProperty(apiCode.msgId));
    }

    @DeleteMapping("programs/{id}")
    @ApiOperation(value = "删除节目信息", notes = "删除节目信息")
    @ApiImplicitParam(paramType = "path", name = "id", dataType = "String", required = true, value = "节目id")
    public ApiResult delete(@PathVariable("id") String id) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, iProgramService.deleteProgram(id));
    }

    @PutMapping("program")
    @ApiOperation(value = "更新节目信息", notes = "更新节目信息")
    @ApiImplicitParam(paramType = "body", name = "programModel", dataType = "Map", required = true, value = "节目实体详细信息")
    public ApiResult updateModel(@RequestBody @Validated ProgramModel programModel, BindingResult bindingResult) {
        ApiCode apiCode = iProgramService.saveProgram(programModel);
        return new ApiResult<>(Integer.parseInt(apiCode.msgId),
                apiCode.equals(ApiCode.APP_300501),
                MsgPropertyUtil.getProperty(apiCode.msgId));
    }

    @PatchMapping("program")
    @ApiOperation(value = "节目信息部分更新", notes = "节目信息部分更新")
    @ApiImplicitParam(paramType = "body", name = "ggModel", dataType = "Map", required = true, value = "节目信息")
    public ApiResult updateProgram(@RequestBody @Validated ProgramModel programModel, BindingResult bindingResult) {
        ApiCode apiCode = iProgramService.saveProgram(programModel);
        return new ApiResult<>(Integer.parseInt(apiCode.msgId),
                apiCode.equals(ApiCode.APP_300501),
                MsgPropertyUtil.getProperty(apiCode.msgId));
    }

    @ResponseBody
    @RequestMapping(value = "programs", method = RequestMethod.GET)
    @ApiOperation("获取所有节目信息")
    public ApiResult findPrograms(@ApiQuery ApiQueryBase query, HttpServletRequest request) {
        query.getConditions().put("devsn", request.getParameter("devsn"));
        query.getConditions().put("title", request.getParameter("title"));
        return new ApiResult<>(AppConstant.MSG_SUCCESS, true,"获取节目信息成功",iProgramService.findPrograms(query));
    }

    @ResponseBody
    @RequestMapping(value = "programs/page", method = RequestMethod.GET)
    @ApiOperation("获取带分页的公告信息")
    public ApiResult findProgramsPage(@ApiQuery ApiQueryBase query, HttpServletRequest request) {

        // 查询条件接受
        query.getConditions().put("ps", request.getParameter("ps"));
        query.getConditions().put("pi", request.getParameter("pi"));
        query.getConditions().put("devsn", request.getParameter("devsn"));
        query.getConditions().put("title", request.getParameter("title"));


        // 翻页对象构建
        PageHelper.startPage(query.getPi(), query.getPs(), true);
        Page<ProgramModel> page = (Page<ProgramModel>) iProgramService.findPrograms(query);
        ApiPager<ProgramModel> apiPager = new ApiPager<ProgramModel>(query.getPs(),
                query.getPi(),
                page.getTotal(),
                page.getResult());

        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", apiPager);

    }

    @ResponseBody
    @RequestMapping(value = "programs/{id}", method = GET)
    @ApiImplicitParam(paramType = "String", name = "id", dataType = "String", required = true, value = "节目ID")
    @ApiOperation("获取单个节目信息")
    public ApiResult findByProgramId(@PathVariable("id") String id) {
        return iProgramService.findProgram(id);
    }

    @ResponseBody
    @RequestMapping(value = "programs/{programid}/files", method = RequestMethod.GET)
    @ApiOperation("获取素材信息")
    public ApiResult findProgramFiles(@PathVariable("programid") String programid) {
        return new ApiResult(AppConstant.MSG_SUCCESS, true, "", iProgramService.findProgramFiles(programid));
    }
    // endregion

}